﻿package game.account.friends {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import game.ui.Text;
	import game.ui.Button;
	import game.ui.DynamicClip;
	import game.objects.DynamicObject;
	import game.ui.Slider;
	import game.objects.UserObject;
	import flash.display.MovieClip;
	import game.objects.AutoObject;
	
	public class Friends extends DynamicClip {
		
		public static const PAGE_TOP:String		= 'page.top';
		public static const PAGE_FRIENDS:String	= 'page.friends';
		public static const PAGE_CARS:String	= 'page.cars';
		
		private function get mark3():Button			{ return getChildByName('mark3')			as Button;	}
		private function get mark2():Button			{ return getChildByName('mark2')			as Button;	}
		private function get mark1():Button			{ return getChildByName('mark1')			as Button;	}
		private function get btnLeftStep():Button	{ return getChildByName('btnLeftStep')		as Button;	}
		private function get btnLeftSteps():Button	{ return getChildByName('btnLeftSteps')		as Button;	}
		private function get btnRightStep():Button	{ return getChildByName('btnRightStep')		as Button;	}
		private function get btnRightSteps():Button	{ return getChildByName('btnRightSteps')	as Button;	}
		private function get slider():Slider		{ return getChildByName('slider')			as Slider;	}
		
		public var current:String	= PAGE_FRIENDS;
		public var marks:Array;
		
		public function Friends():void {
			var skin:DynamicObject = config.getSkin(DynamicObject.FRIENDS);
			
			super(skin);
			
			if (!skin) return;
			
			
		
			
			marks = [{page: PAGE_FRIENDS, button: mark1}, { page: PAGE_TOP, button: mark2}, {page: PAGE_CARS, button: mark3}];
			
			if (btnLeftStep)
				btnLeftStep.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(-1);			});
			if (btnLeftSteps)
				btnLeftSteps.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(-slider.length);	});
			if (btnRightStep)
				btnRightStep.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(1);			});
			if (btnRightSteps)
				btnRightSteps.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(slider.length);		});
			for (var i:String in marks) {
				//trace (marks[i].button, marks[i].page);
				(marks[i].button as Button).name = marks[i].page;
				(marks[i].button as Button).setIDS("IDS_FR_M"+String(parseInt(i)+1));
				(marks[i].button as Button).addEventListener(MouseEvent.CLICK, markHandler);
			}
			
			createPage(current);
			
		}
		
		private function createPage(page:String):void {
			current = page;
			//выбор закладки
			clickMark(page)
			//добавляем слайдер
			insertSlider();
		}
		
		private function clickMark(page:String):void {
			var button:Button;
			for each (var bobj:Object in marks) {
				button = bobj.button as Button;
				if (page==bobj.page) {
					swapTop(button);
				}
				button.enabled = (page!=bobj.page);
				button.alpha = 1;
			}
		}
		
		private function markHandler(event:MouseEvent):void {
			var button:Button = event.target as Button;
			if (!button) return;
			
			createPage(button.name);
		}
		
		private function swapTop(button:Button):void {
			var p:MovieClip = button.parent as MovieClip;
			if (!p) return;
			
			p.swapChildren(button, p.getChildAt(p.numChildren-1));
		}
		
		private function insertSlider():void {
			slider.clear();
			switch (current) {
				case PAGE_FRIENDS:
					insertMyFriends();
				break;
				case PAGE_TOP:
					insertTopFriends();
				break;
				case PAGE_CARS:
					insertCars();
				break;
			}
			
		}
		
		//вставляем машины
		private function insertCars():void {
			for each (var auto:AutoObject in config.me.autos) {
				slider.add( new Car(auto) );
			}
			
			var empty:Car = new Car(null);
			empty.buttonMode = true;
			empty.addEventListener(MouseEvent.CLICK, addSlotHandler);
			slider.add( empty );
		}
		
		//вставляем топ друзей
		private function insertTopFriends():void {
			for each (var user:UserObject in config.me.friends) {
				slider.add( new Friend(user) );
			}
		}
		
		//вставляем друзей
		private function insertMyFriends():void {
			
			for each (var user:UserObject in config.me.friends) {
				slider.add( new Friend(user) );
			}
			
			var empty:Friend = new Friend(null);
			empty.buttonMode = true;
			empty.addEventListener(MouseEvent.CLICK, inviteHandler);
			slider.add( empty );
		}
		
		public function step(value:int):void {
			if (slider)
				slider.step(value);
		}
		
		private function addSlotHandler(event:MouseEvent):void {
			this.dispatchEvent( new FriendsEvent ( FriendsEvent.ADD_SLOT ) );
		}
		
		private function inviteHandler(event:MouseEvent):void {
			this.dispatchEvent( new FriendsEvent ( FriendsEvent.INVITE ) );
		}
	}
	
}